package com.zyw.module.console.sys.controller;

import com.zyw.module.console.sys.po.Notice;
import com.zyw.module.console.sys.service.NoticeService;
import com.zyw.mvc.servlet.BaseController;
import com.zyw.mvc.support.QueryResult;
import com.zyw.mvc.support.WrappedResult;
import com.zyw.orm.support.page.Page;

import java.util.List;

import com.zyw.security.shiro.web.authc.AuthInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("${path.console.prefix}/notice")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("get")
    @ResponseBody
    public WrappedResult get(Integer id) {
        return wrappedSuccessResult(noticeService.get(id));
    }

    @RequestMapping("save")
    @ResponseBody
    public WrappedResult save(Notice notice) {
        int i = noticeService.insert(notice);
        if (i > 0) {
            return wrappedSuccessResult(null);
        } else {
            return wrappedFaliedResult("保存失败");
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public WrappedResult update(Notice notice) {
        int i = noticeService.update(notice);
        if (i > 0) {
            return wrappedSuccessResult(null);
        } else {
            return wrappedFaliedResult("更新失败");
        }
    }

    @RequestMapping("list")
    @ResponseBody
    public WrappedResult query(com.zyw.orm.support.page.Page<Notice> page, Notice notice) {
        page.setParamBean(notice);
        List<Notice> list = noticeService.findByPage(page);
        QueryResult.QueryItem queryItem = new QueryResult.QueryItem();
        queryItem.setRows(list);
        queryItem.setTotal(page.getTotal());
        return wrappedSuccessResult(queryItem, "查询成功");
    }

    /**
     * 获取最新未读消息，最多展示10条
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public WrappedResult findNewNofities() {
        Page<Notice> page = new Page<>();
        Notice notify = new Notice();
        notify.setStatus("0");
        notify.setDelFlag("0");
        AuthInfo principal = (AuthInfo) SecurityUtils.getSubject().getPrincipal();
        notify.setUserid(principal.getUserid());
        page.setParamBean(notify);

        List<Notice> notifies = noticeService.findByPage(page);
        QueryResult.QueryItem queryItem = new QueryResult.QueryItem();
        queryItem.setRows(notifies);
        queryItem.setTotal(page.getTotal());
        return wrappedSuccessResult(queryItem);
    }
}